package com.nlabs.slickgwt.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;

public class MenuItem extends FocusPanel {

	private HTML m_item_label = new HTML(); 
	private Image m_image = new Image();
	private HorizontalPanel m_hpane = new HorizontalPanel(); 
	private MouseHandler mh = new MouseHandler(); 
	private Menu m_menu = null; 
	 
	
	public MenuItem(String text) {
		
		m_item_label.setHTML(text);
		DOM.setStyleAttribute(m_item_label.getElement(), "padding", "3px");
		setStylePrimaryName("slick-menu-item");
		m_item_label.setStylePrimaryName("slick-menu-item-label");
		m_hpane.setWidth("100%");
		m_hpane.add(m_item_label);
		m_hpane.setCellHorizontalAlignment(m_item_label, HasHorizontalAlignment.ALIGN_LEFT);
		setWidget(m_hpane);
	 
		addMouseOverHandler(mh);
		addMouseOutHandler(mh) ;
		addMouseDownHandler(mh);
		addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event) {
				if(m_menu != null){
					removeStyleDependentName("hover");
		    		DOM.setStyleAttribute(m_item_label.getElement(), "color", "black");
					m_menu.hide(); 
				}
				
			}
	
		}) ;
		
	}
	
	 

	public void setMenu(Menu m){
		
		m_menu = m; 
	}
	
	public void setImage(String url){
		m_image.setUrl(url);
		m_hpane.insert(m_image, 0);
	}
	
	private class MouseHandler implements MouseOverHandler, MouseOutHandler, MouseDownHandler {
	
    	
    	public void onMouseOver(MouseOverEvent event) {
    		addStyleDependentName("hover");
    		DOM.setStyleAttribute(m_item_label.getElement(), "color", "white");
    	}

    	public void onMouseOut(MouseOutEvent event) {
    		removeStyleDependentName("hover");
    		DOM.setStyleAttribute(m_item_label.getElement(), "color", "black");
    	}

    	public void onMouseDown(MouseDownEvent event) {
		
    		event.preventDefault(); 
    		
    		 
    	}

    	 
    }
	
	
}
